package com.hly.musicbackground.model.music.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.Final.SongLinkFinal;
import com.hly.musicbackground.Utils.DozerUtils;
import com.hly.musicbackground.dao.SongSheetDao;
import com.hly.musicbackground.domain.SongLink;
import com.hly.musicbackground.domain.SongSheet;
import com.hly.musicbackground.exception.BusinessException;
import com.hly.musicbackground.model.music.VO.SongSheetListVO;
import com.hly.musicbackground.model.music.VO.SongSheetVO;
import com.hly.musicbackground.model.music.params.SongSheetEmitParams;
import com.hly.musicbackground.model.music.service.SongLinkService;
import com.hly.musicbackground.model.music.service.SongService;
import com.hly.musicbackground.model.music.service.SongSheetService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SongSheetServiceImpl extends ServiceImpl<SongSheetDao, SongSheet> implements SongSheetService {

    private final DozerUtils dozerUtils;
    private final SongService songService;
    private final SongLinkService songLinkService;


    @Override
    public SongSheetListVO getSongSheetList(int userId) {
        SongSheetListVO songSheetListVO = new SongSheetListVO();
        songSheetListVO.setList(lambdaQuery().eq(SongSheet::getUserId, userId)
        .list().stream().map(n -> {
            SongSheetVO convert = dozerUtils.convert(n, SongSheetVO.class);
            long count = songLinkService.count(Wrappers
                    .<SongLink>lambdaQuery()
                    .eq(SongLink::getLink, n.getSongSheetId())
                    .eq(SongLink::getType, SongLinkFinal.SONG_SHEET));
            convert.setSongNum((int) count);
            if(convert.getSongNum() > 0){
                convert.setPic(songService.getSongSheetPic(n.getSongSheetId()));
            }else {
                convert.setPic("https://img.zcool.cn/community/01c8f15aeac135a801207fa16836ae.jpg@1280w_1l_2o_100sh.jpg");
            }
            updateById(n);
            return convert;
        }).collect(Collectors.toList()));
        return songSheetListVO;
    }

    @Override
    public void emitSongSheet(SongSheetEmitParams params, int uid) {
        SongSheet songSheet = null;
        if (params.getSongSheetId() != 0){
            songSheet = lambdaQuery().eq(SongSheet::getSongSheetId,
                    params.getSongSheetId()).eq(SongSheet::getUserId,
                    uid).one();
            if (songSheet == null) throw new BusinessException(Code.BUSINESS_ERR, "无权操作");
            songSheet.setName(params.getName());
            updateById(songSheet);
        }else {
            songSheet = new SongSheet();
            songSheet.setUserId(uid);
            songSheet.setName(params.getName());
            save(songSheet);
        }
    }

    @Override
    public int countList(int userId) {
        long count = count(Wrappers.<SongSheet>lambdaQuery()
                .eq(SongSheet::getUserId, userId));
        return (int) count;
    }

    @Override
    public void delSongSheet(int id) {
        removeById(id);
        songLinkService.delAllSongLink(id, SongLinkFinal.SONG_SHEET);
    }
}
